package com.hz.filemanager.ui.clean;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hz.filemanager.App;
import com.hz.filemanager.BuildConfig;
import com.hz.filemanager.R;
import com.hz.filemanager.ad.AdLoadManager;
import com.hz.filemanager.ad.utils.AnalyticsUtils;
import com.hz.filemanager.adapter.ResultAdapter;
import com.hz.filemanager.model.Constant;
import com.hz.filemanager.model.FileDataManager;
import com.hz.filemanager.model.clean.ResultBean;
import com.hz.filemanager.ui.base.BaseActivity;
import com.hz.filemanager.ui.base.InterruptActivity;
import com.hz.filemanager.utils.LogUtils;
import com.hz.sdka.ada.AdListenera;
import com.hz.sdka.ada.Interstitiala;
import com.hz.sdka.commona.bean.HZAdError;

import java.util.ArrayList;
import java.util.List;

public class CleanResultActivity extends BaseActivity implements ResultAdapter.OnItemClickListener {

    public static final String CLEAN_SIZE = "clean_size";

    public final static String TYPE = "type";
    public final static int TYPE_CLEAN = 1;
    public final static int TYPE_BOOST = 2;
    public final static int TYPE_COOLER = 3;
    public final static int TYPE_BATTERY = 4;

    public final static int TYPE_CLEAN_REP = 11;
    public final static int TYPE_BOOST_REP = 12;
    public final static int TYPE_COOLER_REP = 13;
    public final static int TYPE_BATTERY_REP = 14;

    private TextView resultTv;
    private RecyclerView rv;

    private ResultAdapter adapter;
    private List<ResultBean> datas;
    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                handleBackAction();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_clean_result;
    }

    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        resultTv = findViewById(R.id.result_tv);
        rv = findViewById(R.id.result_rv);

    }

    @Override
    protected void initEvent() {

        type = getIntent().getIntExtra(TYPE, 0);
        int cleanSize = getIntent().getIntExtra(CLEAN_SIZE, 0);
        datas = new ArrayList<>();
        switch (type){
            case TYPE_CLEAN:
            case TYPE_CLEAN_REP:

                toolbar.setTitle(R.string.junk_clean);
                if(cleanSize>1000){

                    resultTv.setText(String.valueOf((float) cleanSize/1000) + "GB");
                }else{
                    resultTv.setText(String.valueOf(cleanSize) + "MB");
                }

                //resultTv.setText("Cleaned "+ cleanSize + "MB");
                if(type == TYPE_CLEAN_REP){
                    resultTv.setText("Optimized");
                }else{
                    AnalyticsUtils.INSTANCE.logEvent("gongnengjg_ql");
                    App.spUtils.putLong(Constant.LAST_CLEAN_TIME, System.currentTimeMillis());
                }

                datas.add(new ResultBean("Phone Boost", R.mipmap.boost_icon, "Boost"));
                datas.add(new ResultBean("Phone Cooler", R.mipmap.cooler_icon, "Cool"));
                datas.add(new ResultBean("Phone Saving", R.mipmap.battery_icon, "Save"));

                break;
            case TYPE_BOOST:
            case TYPE_BOOST_REP:
                toolbar.setTitle(R.string.phone_boost);
                resultTv.setText("RAM freed up!");
                if(type == TYPE_BOOST_REP){
                    resultTv.setText("Optimized");
                }else{
                    AnalyticsUtils.INSTANCE.logEvent("gongnengjg_js");
                    App.spUtils.putLong(Constant.LAST_BOOST_TIME, System.currentTimeMillis());
                }

                datas.add(new ResultBean("Junk Clean", R.mipmap.clean_icon, "Clean"));
                datas.add(new ResultBean("Phone Cooler", R.mipmap.cooler_icon, "Cool"));
                datas.add(new ResultBean("Phone Saving", R.mipmap.battery_icon, "Save"));


                break;
            case TYPE_COOLER:
            case TYPE_COOLER_REP:
                toolbar.setTitle(R.string.phone_cooler);
                resultTv.setText("CPU is cooling down!");
                if(type == TYPE_COOLER_REP){
                    resultTv.setText("Optimized");
                }else{
                    AnalyticsUtils.INSTANCE.logEvent("gongnengjg_jw");
                    App.spUtils.putLong(Constant.LAST_COOLER_TIME, System.currentTimeMillis());
                }

                datas.add(new ResultBean("Junk Clean", R.mipmap.clean_icon, "Clean"));
                datas.add(new ResultBean("Phone Boost", R.mipmap.boost_icon, "Boost"));
                datas.add(new ResultBean("Phone Saving", R.mipmap.battery_icon, "Save"));


                break;
            case TYPE_BATTERY:
            case TYPE_BATTERY_REP:
                toolbar.setTitle(R.string.battery_saver);
                resultTv.setText("Battery optimized");
                if(type == TYPE_BATTERY_REP){
                    resultTv.setText("Optimized");
                }else{
                    AnalyticsUtils.INSTANCE.logEvent("gongnengjg_sd");
                    App.spUtils.putLong(Constant.LAST_BATTERY_TIME, System.currentTimeMillis());
                }

                datas.add(new ResultBean("Junk Clean", R.mipmap.clean_icon, "Clean"));
                datas.add(new ResultBean("Phone Boost", R.mipmap.boost_icon, "Boost"));
                datas.add(new ResultBean("Phone Cooler", R.mipmap.cooler_icon, "Cool"));

                break;
        }


        adapter = new ResultAdapter();
        adapter.setDatas(datas);
        adapter.setOnItemClickListener(this);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv.setAdapter(adapter);
    }

    private void handleBackAction() {

        if(type == TYPE_CLEAN_REP
                || type == TYPE_BOOST_REP
                || type == TYPE_COOLER_REP
                || type == TYPE_BATTERY_REP){
            //重复使用功能 在这里直接弹广告
            LogUtils.e(TAG, "重复使用返回" );
            if(FileDataManager.appConfig.getFile_Config().getCfsy_ad() == 1){
                showAD(true);
            }
        }else{
            LogUtils.e(TAG, "功能结果返回");
            if (App.animationADShow && FileDataManager.appConfig.getFile_Config().getJgfh_ad() == 1) {
                showAD(false);
            }
        }
        finish();
    }

    private void showAD(boolean isRepAction) {
        LogUtils.e(TAG, "isRepAction = " + isRepAction);
        String action = getActionWithType(type);
        if(isRepAction){
            AnalyticsUtils.INSTANCE.logEvent("chongfu_ad_" + action);
        }else{
            AnalyticsUtils.INSTANCE.logEvent("fanhui_ad_" + action);
        }

        if(Interstitiala.getInstance().isReady()){
            if(isRepAction){
                AnalyticsUtils.INSTANCE.logEvent("chongfu_ad_ready_" + action);
            }else{
                AnalyticsUtils.INSTANCE.logEvent("fanhui_ad_ready_" + action);
            }

            AdLoadManager.INSTANCE.showInterstitialAd(CleanResultActivity.this, BuildConfig.CJ_INTERSTITIAL_AD, new AdListenera() {
                @Override
                public void adShow() {
                    if(isRepAction){
                        AnalyticsUtils.INSTANCE.logEvent("chongfu_ad_succeed_" + action);
                    }else{
                        AnalyticsUtils.INSTANCE.logEvent("fanhui_ad_succeed_" + action);
                    }

                }

                @Override
                public void adClose() {

                }

                @Override
                public void adError(HZAdError adError) {

                }
            });
        }
        finish();
    }


    @Override
    public void onItemClick(int position) {

        ResultBean resultBean = datas.get(position);

        if("Junk Clean".equals(resultBean.getName())){

            if(!isSpeedClean()){
                gotoRepAnimation(AnimationActivity.TYPE_CLEAN_REP);
            }else{
                gotoScan(ScanActivity.TYPE_CLEAN);
            }

        }else if("Phone Boost".equals(resultBean.getName())){

            if(!isBoost()){
                gotoRepAnimation(AnimationActivity.TYPE_BOOST_REP);
            }else{
                gotoScan(ScanActivity.TYPE_BOOST);
            }

        }else if("Phone Cooler".equals(resultBean.getName())){

            if(!isCooler()){
                gotoRepAnimation(AnimationActivity.TYPE_COOLER_REP);
            }else{
                gotoScan(ScanActivity.TYPE_COOLER);
            }

        }else if("Phone Saving".equals(resultBean.getName())){

            if(!isBattery()){
                gotoRepAnimation(AnimationActivity.TYPE_BATTERY_REP);
            }else{
                migrateTo(BatteryScanActivity.class);
            }
        }

        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.animationADShow = false;
    }

    @Override
    public void onBackPressed() {
        handleBackAction();
    }

    private void gotoScan(int type) {
        Intent cleanIntent = new Intent(CleanResultActivity.this, ScanActivity.class);
        cleanIntent.putExtra(ScanActivity.TYPE, type);
        startActivity(cleanIntent);
    }

    private void gotoRepAnimation(int type) {
        Intent repeat = new Intent(CleanResultActivity.this, AnimationActivity.class);
        repeat.putExtra(AnimationActivity.TYPE, type);
        startActivity(repeat);
    }

    private boolean isSpeedClean(){
        long lastTime = App.spUtils.getLong(Constant.LAST_CLEAN_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        return ( time > 5*60*1000);
    }
    private boolean isBoost(){
        long lastTime = App.spUtils.getLong(Constant.LAST_BOOST_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        return ( time > 5*60*1000);
    }
    private boolean isCooler(){
        long lastTime = App.spUtils.getLong(Constant.LAST_COOLER_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        return ( time > 5*60*1000);
    }
    private boolean isBattery(){
        long lastTime = App.spUtils.getLong(Constant.LAST_BATTERY_TIME, 0);
        long time = System.currentTimeMillis() - lastTime;
        return ( time > 5*60*1000);
    }

    public String getActionWithType(int type) {
        String action = "";
        switch (type){
            case TYPE_CLEAN:
            case TYPE_CLEAN_REP:
                action = "ql";
                break;
            case TYPE_BOOST:
            case TYPE_BOOST_REP:
                action = "js";
                break;
            case TYPE_COOLER:
            case TYPE_COOLER_REP:
                action = "jw";
                break;
            case TYPE_BATTERY:
            case TYPE_BATTERY_REP:
                action = "sd";
                break;
        }
        return action;
    }
}
